package com.android.kotlincoroutinemvvmdemo.dialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

abstract class BaseDialog2<T : ViewDataBinding>(
    context: Context,
    theme: Int = 0
) : Dialog(context, theme) {

    protected val mContext = context

    protected lateinit var mBinding : T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.apply {
            decorView.setBackgroundColor(getColorValue(getColorValue()))
            val mAttribute = window!!.attributes
            //通过WindowManager.LayoutParams设置宽高
            mAttribute.width = WindowManager.LayoutParams.MATCH_PARENT
            mAttribute.height = WindowManager.LayoutParams.MATCH_PARENT

            //mAttribute.gravity = Gravity.CENTER

            //mAttribute.horizontalWeight = 1.0F
            //mAttribute.verticalWeight = 1.0F

            //mAttribute.x = 50
            //mAttribute.y = 50

            //取消动画
            mAttribute.windowAnimations = -1
            mAttribute.rotationAnimation = -1

            window!!.attributes = mAttribute
        }

        mBinding =
            DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), null, false)
        setContentView(mBinding.root)

        //setCanceledOnTouchOutside(true) //不起作用
        //setCancelable(true) //不起作用

        initData(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }

    abstract fun getLayoutId(): Int

    abstract fun getColorValue(): Int

    open fun initData(savedInstanceState: Bundle?) {}

    fun getColorValue(colorRes: Int): Int {
        return mContext.resources.getColor(colorRes)
    }
}